---
title: 'Replication file: Negative Political Identities and Costly Political Action: Appendix Section A.13'
author: "Katharina Lawall, Stuart J. Turnbull-Dugarte, Florian Foos \& Josh Townsley"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  pdf_document: default
  bookdown::pdf_document2:
    toc: false
    keep_tex: yes
    latex_engine: xelatex
abstract:
  
keywords: 
mainfont: cochineal
fontsize: 11pt
---


<!-- Set your working directory here -->
```{r setup, include=FALSE, cache = FALSE}
#install.packages("knitr")
rm(list=ls())
require("knitr")
#knitr::opts_knit$set(root.dir = "C:/Users/katharina.lawall/Downloads/NegativePoliticalIdentities")


```


<!--Load all the required libraries in -->

```{r libraries, include=FALSE, cache = FALSE}

library(margins)



library(ri2)
library(estimatr)

library(texreg)
library(dplyr)
library(tidyr)

library(ggplot2)
library(stringr)

library(randomizr)
library(estimatr)



library(purrr)

library(ggpubr)


library(stargazer)

library(jtools)

#If you get an error message here, you probably need to install the package on your devive, e.g. > install.packages("margins")
```

<!--Loading in survey data -->

```{r dataload, , echo=FALSE}

data <- read.csv("data/surveydata.csv")
```





# Table A.15: Balance table

```{r balancetab, echo=F}



my_summarise <- function(data, group_var, summarise_var) {
  data %>%
    group_by(across({{ group_var }})) %>% 
    summarise(across({{ summarise_var }}, ~ paste(round(mean(., na.rm = T), digits = 2), " (", round(sd(., na.rm = T),digits = 2), ")", sep=""), .names = "{.col}")) #get mean and SD for each variable
}

balancen <- data %>% group_by(treatment_cat) %>% count() %>% pivot_longer(cols= !treatment_cat, names_to="variable",values_to="value") %>%
pivot_wider(names_from = treatment_cat, values_from =value) %>%
  rename("Variable"="variable") 

 
  
balancetab <-  my_summarise(data, treatment_cat,  c(dem_age, dem_gender_female, dem_employment_fulltime)) %>%
pivot_longer(cols= !treatment_cat, names_to="variable",values_to="value") %>%
pivot_wider(names_from = treatment_cat, values_from =value) %>%
  rename("Variable"="variable")  %>%
  mutate(variable = case_when(Variable=="dem_age" ~"Age",
                   Variable=="dem_gender_female" ~"% Female",
                   Variable=="dem_employment_fulltime" ~"% Full-time employed")) %>%
  dplyr::select(-Variable) %>%
  rename("Variable"="variable") %>%
  relocate(Variable)

balance <- rbind(balancetab, balancen)

  




stargazer(balance, summary = F, rownames = F, title="Survey: Balance on key pre-treatment covariates", out="output/si_surveybalance.tex", notes = c("Mean and (SD) for key covariates."))


```





# Figure A.12: Perceived email topic = political parties


```{r figa12polparty, echo=F, warning=F}




summary <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(topic_polparty ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))



data$predicted <- data$topic_polparty


predplot <- ggplot(data, aes(x=treatment_cat, y=predicted)) +
  theme_minimal() +
  geom_jitter(aes(color=treatment_cat),height=.1, width=.2, alpha=.3) +
  geom_point(data = summary, size = 2) +
  geom_errorbar(data = summary,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summary, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
    labs(x = NULL, y = "To what extent does the campaign email make you think \n about political parties? 0 (Not at all) to 10 (A lot)" , title="i) Mean perceived email topic: Political parties \n by treatment condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0,10)  +
    theme_minimal() +
  theme(
plot.title = element_text (size=12, face="bold"),
axis.text.x = element_text(size=12), 
axis.text.y = element_text(size=12),
axis.title.y= element_text(size=12),
legend.position = "none") +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_color_manual(values = c("Party-Positive" = "#f1c232", "Issue-Positive" = "#e69138", "Control" = "#666666" , "Party-Negative"= '#21719e', "Issue-Negative"= '#2a94cf'))

lmplot <- lm_robust(topic_polparty ~ treat_target_ , data=data)


marginssum <- summary(margins(lmplot, variables = "treat_target_"))

meplot <- ggplot(marginssum, aes(x=factor, y=AME)) + 
   geom_point(size=2.5)+
  geom_errorbar(aes(ymin=lower, ymax=upper), width=0, size=0.6, alpha=0.5) +
  ggtitle(paste("ii) Marginal effect of the party vs issue cue on \n", "political parties as the topic of the email ", sep = " "), subtitle = ) +
  xlab("") + ylab("") + labs(caption = "95% CI") +
  scale_y_continuous(limits = c(-1, 3)) +
    theme_minimal() +
    geom_hline(yintercept=0, color="red") +
  theme(
plot.title = element_text (size=12, face="bold"),
axis.text.x = element_blank(), 
axis.text.y = element_text(size=12),
legend.position = "none")




p3 <- ggarrange(predplot, meplot, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")



p3



ggsave("output/si_figa12_manip_topic_polparties.png",width =24, height = 19, units = "cm", dpi=320)



```




\FloatBarrier

# Figure A.13: Perceived email topic = Brexit/EU



```{r figa13brexit, echo=F, warning=F}





#mean values and CI
summary_good <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(topic_brexit ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))






data$predicted <- data$topic_brexit


predplot <- ggplot(data, aes(x=treatment_cat, y=predicted)) +
  theme_minimal() +
  geom_jitter(aes(color=treatment_cat),height=.1, width=.2, alpha=.3) +
  geom_point(data = summary_good, size = 2) +
  geom_errorbar(data = summary_good,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summary_good, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  theme(
plot.title = element_text(hjust = 0, size = 14, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 9),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=9)
) +
    labs(x = NULL, y = "To what extent does the campaign email make you think \n about Brexit? 0 (Not at all) to 10 (A lot)" , title="i) Mean perceived email topic: Brexit \n by treatment condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0,10)  +
    theme_minimal() +
  theme(
plot.title = element_text (size=12, face="bold"),
axis.text.x = element_text(size=12), 
axis.text.y = element_text(size=12),
axis.title.y= element_text(size=12),
legend.position = "none") +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_color_manual(values = c("Party-Positive" = "#f1c232", "Issue-Positive" = "#e69138", "Control" = "#666666" , "Party-Negative"= '#21719e', "Issue-Negative"= '#2a94cf'))


predplot


lmplot <- lm_robust(topic_brexit ~ treat_target_issue , data=data)
#Step 2: get the predicted values from this model and save them to a dataframe 




marginssum <- summary(margins(lmplot, variables = "treat_target_issue"))

meplot <- ggplot(marginssum, aes(x=factor, y=AME)) + 
   geom_point(size=2.5)+
  geom_errorbar(aes(ymin=lower, ymax=upper), width=0, size=0.6, alpha=0.5) +
  ggtitle(paste("ii) Marginal effect of the issue vs party cue on \n", "Brexit as the topic of the email ", sep = " "), subtitle = ) +
  xlab("") + ylab("") + labs(caption = "95% CI") +
  scale_y_continuous(limits = c(-1, 5)) +
    theme_minimal() +
    geom_hline(yintercept=0, color="red") +
  theme(
plot.title = element_text (size=12, face="bold"),
axis.text.x = element_blank(), 
axis.text.y = element_text(size=12),
legend.position = "none")




p3 <- ggarrange(predplot, meplot, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")



p3

ggsave("output/si_figa13_manip_topic_brexit.png", width =24, height = 19, units = "cm", dpi=320)


```


# Figure A.14: Negative political identity



```{r figa14negpolid, echo=F, warning=F}




#mean values and CI
summary_good <-
  data %>% group_by(treatment_cat) %>%
  do(tidy(lm_robust(polid_negative ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722))


data$predicted <- data$polid_negative


predplot <- ggplot(data, aes(x=treatment_cat, y=predicted)) +
  theme_minimal() +
  geom_jitter(aes(color=treatment_cat),height=.1, width=.2, alpha=.3) +
  geom_point(data = summary_good, size = 2) +
  geom_errorbar(data = summary_good,
                aes(ymin = conf.low, ymax = conf.high), colour= "black", width=0, size=0.9, alpha=0.5) +
  geom_linerange(data=summary_good, aes(ymin=conf.low.83, ymax=conf.high.83), width=.3, size=1.5, alpha=1.5) +
  labs(x = NULL, y=  "Proportion of respondents who agree that the email appeals to people with \n a Negative political identity (1) vs Positive identity/DK (0)", title="i) Mean perceived negative identity of the email \n by treatment condition", caption = "83% CI (thick) and 95% CI (thin)")+
    ylim(0,1)  +
    theme_minimal() +
  theme(
plot.title = element_text (size=12, face="bold"),
plot.subtitle = element_text (size=12),
axis.text.x = element_text(size=12), 
axis.text.y = element_text(size=12),
axis.title.y= element_text(size=12),
legend.position = "none") +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_color_manual(values = c("Party-Positive" = "#f1c232", "Issue-Positive" = "#e69138", "Control" = "#666666" , "Party-Negative"= '#21719e', "Issue-Negative"= '#2a94cf'))


lmplot <- lm_robust(polid_negative ~ treat_id_negative , data=data)
#Step 2: get the predicted values from this model and save them to a dataframe 




marginssum <- summary(margins(lmplot, variables = "treat_id_negative"))

meplot <- ggplot(marginssum, aes(x=factor, y=AME)) + 
   geom_point(size=2.5)+
  geom_errorbar(aes(ymin=lower, ymax=upper), width=0, size=0.6, alpha=0.5) +
  ggtitle(paste("ii) Marginal effect of the negative vs positive \n ", "cue on perceived negative identity", sep = " "), subtitle = ) +
  xlab("") + ylab("") + labs(caption = "95% CI") +
  scale_y_continuous(limits = c(-0.5, 1)) +
    theme_minimal() +
    geom_hline(yintercept=0, color="red") +
  theme(
plot.title = element_text (size=12, face="bold"),
axis.text.x = element_blank(), 
axis.text.y = element_text(size=12),
legend.position = "none")




p3 <- ggarrange(predplot, meplot, ncol=2, nrow=1, widths = c(1.1, 1), align="hv")





ggsave("output/si_figa14_manip_negid.png",width =24, height = 19, units = "cm", dpi=320)


```






# Figure A.15: Who is meant by the we in the email?



```{r figa15we, echo=F, warning=F}



dat <- data %>%
  group_by(treatment_cat, we_id) %>%
  summarise(n = n()) %>%
  mutate(prop = n/sum(n),
         upperE=1.96*sqrt(n/sum(n)*(1-(n/sum(n)))/sum(n)), 
         lowerE=-1.96*sqrt(n/sum(n)*(1-(n/sum(n)))/sum(n)),
         ymin = prop+lowerE, 
         ymax = prop+upperE,
         ymin= replace(ymin, ymin<0, 0))



ggplot(dat, aes(x=treatment_cat, y = prop, fill = we_id, pattern=we_id))+ 
  geom_bar(position = "dodge", stat = "identity") + geom_errorbar(aes(ymin = ymin, ymax = ymax),width = .2, position=position_dodge(.9))+
  labs(x="Treatment condition",y="Proportion of respondents") +
  ylim(0,1) +
  ggtitle("Who do you think is mainly meant by the “we” in the email?")+
  theme_minimal() +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  scale_fill_manual(values = alpha(c("#ffffbf", '#21719e', '#abd9e9',"#fdae61","#0b0a0a"), 1 ), name="People who identify as...") +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) 




ggsave("output/si_figa15_manip_weid.png",width =24, height = 19, units = "cm", dpi=320)



```


